<template>
    <el-card style="margin-top:10px">
        <el-button type="primary" :icon="Plus" style="margin-bottom:20px" @click="toAddSpu"
            :disabled="!categoryStore.category3Id">添加SPU</el-button>
        <el-table :data="spuList" border>
            <el-table-column type="index" width="80px" label="序号" align="center" />
            <el-table-column label="SPU名称" prop="spuName"></el-table-column>
            <el-table-column label="SPU描述" prop="description"></el-table-column>
            <el-table-column label="操作">
                <template v-slot="{ row }">
                    <el-button type="primary" size="small" title="添加SKU" :icon="Plus" @click="toAddSku(row)"></el-button>
                    <el-button type="primary" size="small" title="修改SPU" :icon="Edit"
                        @click="toEditeSkuInfo(row)"></el-button>
                    <el-tooltip class="box-item" effect="dark" content="查看SKU列表" placement="top">
                        <el-button type="info" size="small" title="查看SKU列表" :icon="InfoFilled"
                            @click="toCheckSkuList(row.id)"></el-button>
                    </el-tooltip>
                    <el-popconfirm confirm-button-text="确定" cancel-button-text="取消" :icon="InfoFilled" icon-color="#626AEF"
                        title="真的确定要删除此条数据么?" @confirm="deleteSpuInfo(row.id)">
                        <template #reference>
                            <el-button type="danger" size="small" title="删除SPU" :icon="Delete"></el-button>
                        </template>
                    </el-popconfirm>
                </template>
            </el-table-column>


        </el-table>
        <!-- 分页器 -->
        <el-pagination v-model:current-page="currentPage" v-model:page-size="pageSize" :page-sizes="[3, 5, 10, 20]"
            :small="small" :disabled="disabled" :background="background" layout="prev, pager, next, jumper,->,sizes,total"
            :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange" style="margin-top:20px" />
        <!-- 查看sku -->
        <el-dialog v-model="dialogSkuVisible">
            <el-table :data="skuList">
                <el-table-column property="skuName" label="名称" width="150" />
                <el-table-column property="price" label="价格" width="200" />
                <el-table-column property="weight" label="重量(千克)" />
                <el-table-column label="默认图片">
                    <template v-slot="{ row }">
                        <el-image style="width: 100px; height: 100px" :src="row.skuDefaultImg" />
                    </template>
                </el-table-column>
            </el-table>
        </el-dialog>
    </el-card>
</template>
  
<script lang="ts">
export default {
    name: 'SpuList'

}


</script>

<script setup lang="ts">
import { Plus, Edit, Delete, InfoFilled } from "@element-plus/icons-vue";
import { ref, watch } from 'vue'
import { useCategoryListStore } from '@/stores/categoryList';
import { reqGetgetSpuListData, reqDeleteSpu } from '@/api/product/spu'
import { ShowStatus } from "./types";
import { ElMessage } from 'element-plus';
import type { SpuInfoModel } from '@/api/product/model/spuModel';
import { reqGetSkuListData } from '@/api/product/sku';
const currentPage = ref<number>(1); //当前页
const pageSize = ref<number>(3); //每页显示条数值
const small = ref<boolean>(false); //是否使用小型分页样式
const background = ref<boolean>(false); //是否为分页按钮添加背景色
const disabled = ref<boolean>(false); //是否禁用分页
const total = ref<number>(0); //数据的总条数

const spuList = ref<SpuInfoModel[]>([]) //spu列表数据

const emits = defineEmits(['changStatus', 'getSpuInfo'])

// const showStatus = ref<number>(ShowStatus.ADD_SPU);

//查看sku列表是否显示
const dialogSkuVisible = ref(false)
//sku信息
const skuList = ref([])


//引入store
const categoryStore = useCategoryListStore()

//获取sku列表数据的函数
const getSpuListData = async () => {
    const result = await reqGetgetSpuListData(currentPage.value, pageSize.value, categoryStore.category3Id as number)
    // console.log(result)
    spuList.value = result.records
    total.value = result.total
}

//监听三级导航获取列表数据
watch(() => categoryStore.category3Id, (category3Id) => {
    if (!category3Id) {
        return spuList.value = []
    }

    getSpuListData()
}, { immediate: true })


//当前页面的显示数量发生变化时
const handleSizeChange = (val: number) => {
    pageSize.value = val;
    currentPage.value = 1;
    //重新获取页面列表数据
    getSpuListData()
};

//当前页发生变化时
const handleCurrentChange = (val: number) => {
    currentPage.value = val;
    //重新获取页面列表数据
    getSpuListData()
};

//添加spu
const toAddSpu = () => {
    //点击添加时，先清空spuInfo旧值
    emits('getSpuInfo', { spuImageList: [], spuSaleAttrList: [] })
    //改变当前显示的组件为spuForm
    emits('changStatus', ShowStatus.ADD_SPU)
}

//添加sku
const toAddSku = (spuInfo: SpuInfoModel) => {
    emits('changStatus', ShowStatus.ADD_SKU)
    emits('getSpuInfo', spuInfo)
}

//删除spu
const deleteSpuInfo = async (id: number) => {
    await reqDeleteSpu(id)
    ElMessage.success('删除spu成功')
    getSpuListData()
}

//修改spu信息
const toEditeSkuInfo = (spuInfo: SpuInfoModel) => {

    //传递spuInfo信息给index组件
    emits('getSpuInfo', {
        ...spuInfo,
        spuImageList: [],
        spuSaleAttrList: []
    })
    //跳转到spuForm页面
    emits('changStatus', ShowStatus.ADD_SPU)
}

//查看sku
const toCheckSkuList = async (spuId: number) => {
    const result = await reqGetSkuListData(spuId)
    dialogSkuVisible.value = true
    skuList.value = result
}
</script>
  
<style></style>